package com.example.jussi.myproject;

import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    List<CardBean> cardList = new ArrayList<>();
    CardBean cardBean;
    MyDatabase myDatabase;
    SQLiteDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDatabase = new MyDatabase(MainActivity.this);
        database = myDatabase.getWritableDatabase();
    }

    public void add(View view) {
        Intent intent = new Intent(MainActivity.this, AddCard.class);
        startActivityForResult(intent, 1);
    }

    public void find(View view){
        cardList.clear();
        String sql = "select * from cards";
        Cursor cursor = database.rawQuery(sql, null);
        while (cursor.moveToNext()){
            cardBean = new CardBean();
            cardBean.setId(cursor.getInt(cursor.getColumnIndex("id")));
            cardBean.setBusinessName(cursor.getString(cursor.getColumnIndex("business_name")));
            cardBean.setName(cursor.getString(cursor.getColumnIndex("name")));
            cardBean.setJob(cursor.getString(cursor.getColumnIndex("job")));
            cardBean.setEmail(cursor.getString(cursor.getColumnIndex("email")));
            cardBean.setPhoneNum(cursor.getString(cursor.getColumnIndex("phone")));
            cardBean.setQqNum(cursor.getString(cursor.getColumnIndex("qqnumber")));
            cardBean.setInfo(cursor.getString(cursor.getColumnIndex("info")));
            cardBean.setAddr(cursor.getString(cursor.getColumnIndex("address")));
            cardList.add(cardBean);
        }
        Intent intent = new Intent(MainActivity.this, CardList.class);
        intent.putExtra("cardList", (Serializable) cardList);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    cardBean = (CardBean) data.getSerializableExtra("card");
                    String sql = "insert into cards(business_name, name, job, email, phone, qqnumber, info, address) values(?, ?, ?, ?, ?, ?, ?, ?)";
                    try {
                        database.execSQL(sql, new Object[]{
                                cardBean.getBusinessName(),
                                cardBean.getName(),
                                cardBean.getJob(),
                                cardBean.getEmail(),
                                cardBean.getPhoneNum(),
                                cardBean.getQqNum(),
                                cardBean.getInfo(),
                                cardBean.getAddr()
                        });
                        Toast.makeText(MainActivity.this, "名片插入成功！！！", Toast.LENGTH_SHORT).show();
                    } catch (SQLException e) {
                        e.printStackTrace();
                        Toast.makeText(MainActivity.this, "名片插入失败！！！", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(MainActivity.this, "您取消了本次操作！！！", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
}
